<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>编辑通讯录分类</title>

    <!-- bootstrap -->
    <link rel="stylesheet" href="./assets/css/bootstrap.min.css" />

    <!-- animate.css -->
    <link rel="stylesheet" href="./assets/css/animate.min.css" />

    <!-- bootstrap-validate -->
    <link rel="stylesheet" href="./assets/plugins/bootstrap-validator/bootstrapValidator.min.css" />

    <!-- mui -->
    <link rel="stylesheet" href="./assets/plugins/mui/css/mui.min.css" />

    <!-- 重置样式 -->
    <link rel="stylesheet/less" href="./assets/css/reset.less" />

    <!-- 公共样式 -->
    <link rel="stylesheet/less" href="./assets/css/common.less" />

    <!-- 页面独立的 -->
    <link rel="stylesheet/less" href="./assets/css/ContactAdd.less" />
</head>
<body>
    <!-- 信息编辑 -->
    <div class="info">
        <h3 class="title wow fadeInUp">编辑通讯录分类</h3>

        <p class="desc wow fadeInUp" data-wow-delay="200ms">
            您可在本页编辑您的通讯录分类，编辑成功后通讯录分类将会出现在首页。
        </p>
    </div>

    <form id="edit" class="contact wow fadeInUp" data-wow-delay="300ms">
        <div class="form-group">
            <label for="type-name">分类名称</label>
            <input type="text" class="form-control" name="name" id="type-name" placeholder="请输入分类名称" />
        </div>
        
        <div class="action">
            <button class="btn">提交</button>
        </div>
    </form>
</body>
</html>
<!-- 解析less的文件 -->
<script src="./assets/js/less.min.js"></script>

<!-- jquery  -->
<script src="./assets/js/jquery.min.js"></script>

<!-- bootstrap -->
<script src="./assets/js/bootstrap.min.js"></script>

<!-- wow.js -->
<script src="./assets/js/wow.min.js"></script>

<!-- bootstrap-validate -->
<script src="./assets/plugins/bootstrap-validator/bootstrapValidator.min.js"></script>

<!-- mui -->
<script src="./assets/plugins/mui/js/mui.min.js"></script>

<!-- axios -->
<script src="./assets/plugins/axios/axios.min.js"></script>

<!-- 服务请求 -->
<script src="./assets/plugins/axios/request.js"></script>

<!-- cookie -->
<script src="./assets/plugins/cookie.js"></script>

<!-- helpers -->
<script src="./assets/plugins/helpers.js"></script>

<script>
    //实例化wow.js
    new WOW().init();

    // 获取ID
    let id = getParam('id');

    let business = {};

    (async () => {
        let result = await check(true,true);

        business = result.data;

        getTypeInfo();
    })();

    // 获取通讯录分类详情数据
    async function getTypeInfo()
    {
        let params = {
            businessid:business.id,
            typeid:id
        }

        let result = await POST({
            url:'/card/type/check',
            params
        })

        if(result.code === 1)
        {
            let info = result.data;

            $('#type-name').val(info.name);

            return false;
        }else{
            mui.toast(result.msg,{duration:1500});

            setTimeout(() => {
                location.href = 'TypeIndex.html';
            },1500);

            return false;
        }
    }
    
    // 实例化验证器
    $('#edit').bootstrapValidator({
        // 填写配置项
        message:'表单内容不能为空',// 所有字段默认错误信息
        // 字段校验图标。默认bootstrap图标
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields:{
            name:{
                validators:{
                    // 必填
                    notEmpty:{
                        message: '分类名称不能为空'
                    }
                }
            }
        }
    }).on('success.form.bv',function(e){
        // 阻止多次提交
        e.preventDefault();
    });

    // 表单提交
    $('#edit').on('submit',async function(){
        // 判断表单验证是否失败
        if(!$('#edit').data('bootstrapValidator').isValid())
        {
            // 重置表单内容，包括错误信息
            // $('#edit').data('bootstrapValidator').resetForm();

            // 验证没有通过就不执行后面代码
            return false;
        }

        // 组装数据
        let params = {
            typeid:id,
            businessid:business.id,
            name:$.trim($('#type-name').val())
        }

        let result = await POST({
            url:'/card/type/edit',
            params
        })

        if(result.code === 1)
        {
            mui.toast(result.msg,{duration:1500});

            setTimeout(() => {
                location.href = 'TypeIndex.html';
            },1500);

            return false;
        }else{
            mui.toast(result.msg,{duration:1500});

            return false;
        }
    })

</script>